// Copyright (c) 2022, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// Returns true if the [value] has emoji characters in it.
bool hasEmojiCharacter(String value) {
  for (final c in value.runes) {
    if (c < 256) continue;
    if (_basicEmojiRanges.any((r) => r.start <= c && c < r.end)) {
      return true;
    }
  }
  return false;
}

/// Ranges were generated by the `_UnicodeEmojiSequences.getBasicRanges`
/// method, with a merge threshold of 4. It collapses the ranges with a
/// reasonable rate where we may include characters that are not necessarily
/// emojis.
///
/// print(sequences.getBasicRanges(mergeThreshold: 4).map((e) => '$e,\n').join());
final _basicEmojiRanges = <CodeUnitRange>[
  CodeUnitRange(0x231a, 0x231c),
  CodeUnitRange(0x23e9, 0x23f4),
  CodeUnitRange(0x25fd, 0x25ff),
  CodeUnitRange(0x2614, 0x2616),
  CodeUnitRange(0x2648, 0x2654),
  CodeUnitRange(0x267f, 0x2680),
  CodeUnitRange(0x2693, 0x2694),
  CodeUnitRange(0x26a1, 0x26a2),
  CodeUnitRange(0x26aa, 0x26ac),
  CodeUnitRange(0x26bd, 0x26bf),
  CodeUnitRange(0x26c4, 0x26c6),
  CodeUnitRange(0x26ce, 0x26cf),
  CodeUnitRange(0x26d4, 0x26d5),
  CodeUnitRange(0x26ea, 0x26eb),
  CodeUnitRange(0x26f2, 0x26fe),
  CodeUnitRange(0x2705, 0x270c),
  CodeUnitRange(0x2728, 0x2729),
  CodeUnitRange(0x274c, 0x2758),
  CodeUnitRange(0x2795, 0x2798),
  CodeUnitRange(0x27b0, 0x27b1),
  CodeUnitRange(0x27bf, 0x27c0),
  CodeUnitRange(0x2b1b, 0x2b1d),
  CodeUnitRange(0x2b50, 0x2b56),
  CodeUnitRange(0x1f004, 0x1f005),
  CodeUnitRange(0x1f0cf, 0x1f0d0),
  CodeUnitRange(0x1f18e, 0x1f19b),
  CodeUnitRange(0x1f201, 0x1f202),
  CodeUnitRange(0x1f21a, 0x1f21b),
  CodeUnitRange(0x1f22f, 0x1f23b),
  CodeUnitRange(0x1f250, 0x1f252),
  CodeUnitRange(0x1f300, 0x1f321),
  CodeUnitRange(0x1f32d, 0x1f394),
  CodeUnitRange(0x1f3a0, 0x1f3d4),
  CodeUnitRange(0x1f3e0, 0x1f53e),
  CodeUnitRange(0x1f54b, 0x1f568),
  CodeUnitRange(0x1f57a, 0x1f57b),
  CodeUnitRange(0x1f595, 0x1f597),
  CodeUnitRange(0x1f5a4, 0x1f5a5),
  CodeUnitRange(0x1f5fb, 0x1f650),
  CodeUnitRange(0x1f680, 0x1f6c6),
  CodeUnitRange(0x1f6cc, 0x1f6d8),
  CodeUnitRange(0x1f6dd, 0x1f6e0),
  CodeUnitRange(0x1f6eb, 0x1f6ed),
  CodeUnitRange(0x1f6f4, 0x1f6fd),
  CodeUnitRange(0x1f7e0, 0x1f7f1),
  CodeUnitRange(0x1f90c, 0x1fa00),
  CodeUnitRange(0x1fa70, 0x1fa87),
  CodeUnitRange(0x1fa90, 0x1fabb),
  CodeUnitRange(0x1fac0, 0x1fac6),
  CodeUnitRange(0x1fad0, 0x1fada),
  CodeUnitRange(0x1fae0, 0x1fae8),
  CodeUnitRange(0x1faf0, 0x1faf7),
];

class CodeUnitRange {
  final int start;
  final int end;

  CodeUnitRange(this.start, this.end);

  @override
  String toString() =>
      'CodeUnitRange(0x${start.toRadixString(16)}, 0x${end.toRadixString(16)})';
}
